package br.webchild.server;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

import br.webchild.classes.MedicoClass;
import br.webchild.client.FuncionarioServico;
import br.webchild.client.MedicoServico;
import br.webchild.conexaoBanco.ConexaoBD;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class MedicoServImpl extends RemoteServiceServlet implements MedicoServico, Serializable {
	private static final long serialVersionUID = 1L;
	
	private Connection conecta;
	private PreparedStatement novoadd;
	
	public MedicoServImpl() throws SQLException {
		this.conecta = ConexaoBD.getConexao();
		novoadd = conecta.prepareStatement("INSERT INTO medico("+
	            "med_crm, med_nome, med_especialidade, med_endereco, med_cida_codigo,"+ 
	            "med_numero, med_complemento, med_bairro, med_telcel, med_telfixo,"+ 
	            "med_ativo, med_sexo, med_datacad, med_datanasc, med_rg, med_cpf)"+
	    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
	     
	}

	@Override
	public boolean novo(MedicoClass medicoClass) {
		boolean resposta = true;
		java.sql.Date data = new java.sql.Date(System.currentTimeMillis());		
		int aa = medicoClass.getCrm();
		String a = medicoClass.getNome();
		String b = medicoClass.getEspecialidade();
		String c = medicoClass.getEndereco();
		int d = medicoClass.getCidade();
		int e = medicoClass.getNumero(); 
		String f = medicoClass.getComplemento();
		String g = medicoClass.getBairro();
		String h = medicoClass.getTelCel();
		String i = medicoClass.getTelFixo();
		String j = medicoClass.getAtivo();
		String l = medicoClass.getSexo();
		Date m = medicoClass.getDataCad();
		Date n = medicoClass.getDataNasc();
		String o = medicoClass.getRg();
		String p = medicoClass.getCpf();		
		
		try {				
			novoadd.setInt(1, aa);
			novoadd.setString(2, a);
			novoadd.setString(3, b);
			novoadd.setString(4, c);
			novoadd.setInt(5, 202);
			novoadd.setInt(6, 3);
			novoadd.setString(7, f);
			novoadd.setString(8, g);
			novoadd.setString(9, h);
			novoadd.setString(10, i);
			novoadd.setString(11, j);
			novoadd.setString(12, l);
			novoadd.setDate(13, data);
			novoadd.setDate(14, data);
			novoadd.setString(15, o);
			novoadd.setString(16, p);			
			novoadd.execute();
			//novoadd.close();
			//close();
			System.out.println("><"+aa+"><"+a+"><"+b+"><"+c+"><"+d+"><"+e+"><"+f+"><"+g+"><"+h+"><"+i+"><"+j+"><"+l+"><"+o+"><"+p+"><"+m+"><"+n+"><");
			
		} catch (Exception ex) {
			System.out.println("erro no sql ->" + ex);
			resposta = false;
			close();			
		}finally{
			try {
				novoadd.close();
				close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			
		}
		return resposta;
	}
	
	public void close(){
		try {
			conecta.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}

}
